package cn.zzf.algs.note.base.collection.v1;

/**
 * @author GaoFeng2017
 * @date 2019/11/15 16:18
 */
public class BinarySearch {

    private Integer[] nums;

    public BinarySearch(Integer [] nums) {
        this.nums = nums;
    }

    public int search(int num) {
        int start = 0;
        int end = nums.length - 1;

        while (start <= end) {

            int mid = (start + end) / 2;
            int midNum = nums[mid];

            if (midNum > num) {
                // 往左找
                end = mid - 1;
            } else if (midNum < num){
                // 往右找
                start = mid + 1;
            } else {
                return mid;
            }

        }

        return end < 0 ? end : -end;
    }

    public static void main() {
        // 最后的想不能实现
        // 当然
    }
}

